package mo.daliy;

import java.util.HashSet;
import java.util.Set;

public class L1521 {

    public int closestToTarget(int[] arr, int target) {
        Set<Integer> stack = new HashSet<>();
        Set<Integer> tmp = new HashSet<>();
        int ret = Integer.MAX_VALUE;
        for (int ele : arr) {
            for (int s : stack) {
                tmp.add(s & ele);
            }
            tmp.add(ele);

            for (int s : tmp) {
                ret = Math.min(ret, Math.abs(s - target));
            }

            stack.clear();
            stack.addAll(tmp);
            tmp.clear();
        }
        return ret;
    }

}
